/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class DAOAction extends BaseDAO {
    private Connection con = getConnection();
    private ResultSet rs = null;
    private Statement stmt = null;
    private  PreparedStatement pStmt = null;
    public boolean  insertSubject(Insert in) {
        try {
            pStmt = con.prepareCall("insert into Subject values (?,?)");
            pStmt.setString(1, in.getSub());
            pStmt.setBoolean(2, true);
            if(pStmt.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }
    public boolean  insertSubSubject2(Insert in) {
        try {
            pStmt = con.prepareCall("insert into subSubject values (?,?,?)");
            pStmt.setInt(1, in.getSubID());
            pStmt.setString(2, in.getSubName());
            pStmt.setBoolean(3, true);
            if(pStmt.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }
    public boolean  updateStatusSubject(int id,String display){
        try {
            pStmt = con.prepareCall("UPDATE SUBJECT SET STATUS = ? WHERE SUBJECTID = ?");
            pStmt.setString(1, display);
            pStmt.setInt(2, id);
            if(pStmt.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }
    public boolean  updateStatusSubSubject(int id,String display){
        try {
            pStmt = con.prepareCall("UPDATE SUBSUBJECT SET STATUS = ? WHERE SUBID = ?");
            pStmt.setString(1, display);
            pStmt.setInt(2, id);
            if(pStmt.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }
    public boolean  updateStatusQuestion(int id,String display){
        try {
            pStmt = con.prepareCall("UPDATE Questions SET STATUS = ? WHERE QuestionID = ?");
            pStmt.setString(1, display);
            pStmt.setInt(2, id);
            if(pStmt.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }
    public boolean  updateSubject(int id,Insert in){
        try {
            pStmt = con.prepareCall("UPDATE Subject SET SubjectName = ? Where SubjectID = ?");
            pStmt.setString(1, in.getSub());
            pStmt.setInt(2, id);
            if(pStmt.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }
    public boolean  updateSubSubject(int id,Insert in){
        try {
            pStmt = con.prepareCall("UPDATE subSubject SET SubName = ? Where SubID = ?");
            pStmt.setString(1, in.getSub());
            pStmt.setInt(2, id);
            if(pStmt.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }
    public boolean  insertQuestion(int id,Question q) {
        try {
            pStmt = con.prepareCall("insert into Questions values (?,?,?,?)");
            pStmt.setInt(1, id);
            pStmt.setString(2, q.getQuestion());
            pStmt.setFloat(3,q.getMark());
            pStmt.setBoolean(4,true);
            if(pStmt.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }
    public boolean  insertAnswer(int id,String Answer,String TrueOrFalse) {
        try {
            pStmt = con.prepareCall("insert into Answers values (?,?,?)");
            pStmt.setInt(1, id);
            pStmt.setString(2, Answer);
            pStmt.setString(3,TrueOrFalse);
            if(pStmt.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }
    public boolean  updateAnswer(int id,String a,String t){
        try {
            pStmt = con.prepareCall("UPDATE Answers SET AnswerContent = ? , TrueOrFalse = ? Where AnswerID  = ?");
            pStmt.setString(1, a);
            pStmt.setString(2, t);
            pStmt.setInt(3, id);
            if(pStmt.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }
    public boolean  deleteAnswer(int id){
        try {
            pStmt = con.prepareCall("delete from Answers Where AnswerID = ?");
            pStmt.setInt(1, id);
            if(pStmt.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }
    public boolean  updateQuestion(int id,String a,float  t){
        try {
            pStmt = con.prepareCall("UPDATE Questions SET ContentQuestion = ? , Mark = ? Where QuestionID = ?");
            pStmt.setString(1, a);
            pStmt.setFloat(2, t);
            pStmt.setInt(3, id);
            if(pStmt.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }
    public boolean insertTest(int id,String name,int time) {
         try {
            pStmt = con.prepareCall("insert into Test values (?,?,?,?)");
            pStmt.setInt(1, id);
            pStmt.setString(2, name);
            pStmt.setInt(3, time);
            pStmt.setBoolean(4, true);
            if(pStmt.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }
    public boolean updateTest(int id,String name,int time) {
         try {
            pStmt = con.prepareCall("Update Test Set TestName = ? , TestTime = ? WHERE TestID = ? ");
            pStmt.setString(1,name);
            pStmt.setInt(2, time);
            pStmt.setInt(3, id);
            if(pStmt.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }
    public boolean updateTestStatus(int id,String a) {
         try {
            pStmt = con.prepareCall("Update Test Set Status = ? WHERE TestID = ? ");
            pStmt.setString(1,a);
            pStmt.setInt(2, id);
            if(pStmt.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }
    public boolean  deleteTest(int id){
        try {
            pStmt = con.prepareCall("delete from Test Where TestID = ?");
            pStmt.setInt(1, id);
            if(pStmt.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }
    public boolean  deleteSubject(int id){
        try {
            pStmt = con.prepareCall("delete from Subject Where SubjectID = ?");
            pStmt.setInt(1, id);
            if(pStmt.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }
    public boolean  deleteSubSubject(int id){
        try {
            pStmt = con.prepareCall("delete from subSubject Where SubID = ?");
            pStmt.setInt(1, id);
            if(pStmt.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAOAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }
}
